/*************************************************************************
    > File Name: media.cpp
    > Author: 王杨洋
    > Mail: 2549075913@qq.com
    > Created Time: 2021年02月23日 星期二 14时51分03秒
*************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "../include/message.h"

static int GetChkSum(char* data, int dataLen) {
	int sum = 0;
	for(int i = 0; i < dataLen; i++) {
		sum += data[i];
	}
	return sum;
}

//向服务器请求视频流URL
static int AskForVideo(int fd, char *data, int userLen, char *proxyURL)
{
    MsgStruct msg;
    int curSeq = 0;
    memset(&msg, 0x0, sizeof(MsgStruct));
    msg.version = MSG_VERSION;
	msg.header_len = MSG_HEADER_STABLE_LEN;
	msg.encrypt_type = MSG_ENCRYPT_NONE;
	msg.protocol_type = MSG_PROTOCOL_C2S;
	msg.data_type = MSG_DATA_GET_PROXY_URL;
	msg.seq_num = curSeq;
	msg.frag_flag = MSG_FLAG_FRAG_NO;
	msg.frag_offset = 0;
	msg.total_len = MSG_HEADER_STABLE_LEN;
	msg.source_addr = 0x7F000001;
	msg.target_addr = 0x7F000001;
	msg.header_chk = GetChkSum((char*)(&msg), MSG_HEADER_STABLE_LEN);
	msg.custom1 = userLen;
    strcpy(msg.data, data);
	if(send(fd, &msg, sizeof(MsgStruct), 0) < 0)
	{
		perror("fail to send.\n");
		return -1;
	}
	printf("send ok!\n");
    memset(&msg, 0x0, sizeof(MsgStruct));

	if(recv(fd, &msg, sizeof(MsgStruct), 0) < 0)
	{
		perror("fail to recv.\n");
		return -1;		
	}

	//Check Some Param
	if (msg.version != MSG_VERSION) {
		printf("version error:0x%x.\n", msg.version);
		return -1;		
	}
	if (msg.encrypt_type != MSG_ENCRYPT_NONE) {
		printf("encrypt_type error:0x%x.\n", msg.encrypt_type);
		return -1;		
	}
	if (msg.protocol_type != MSG_PROTOCOL_S2C) {
		printf("protocol_type error:0x%x.\n", msg.protocol_type);
		return -1;	
	}
	if (msg.frag_flag != MSG_FLAG_FRAG_NO) {
		printf("frag_flag error:0x%x.\n", msg.frag_flag);
		return -1;
	}
	if (msg.header_chk != GetChkSum((char*)(&msg), msg.header_len)) {
		printf("header_chk error:0x%x.\n", msg.header_chk);
		return -1;
	}
	//请求失败，打印错误信息
	if (msg.custom1 == MSG_CUSTOM1_GET_PROXY_URL_FAILURE) {
		switch (msg.custom2) {
			case MSG_CUSTOM2_GET_PROXY_URL_ACCOUNT: {
				printf("user don't login.\n");
				return -1;
				} break;
			case MSG_CUSTOM2_GET_PROXY_URL_CONNECT: {
				printf("connect error.\n");
				return -1;
				} break;
			case MSG_CUSTOM2_GET_PROXY_URL_DATABASE: {
				printf("device error.\n");
				return -1;
				} break;
			default: break;
		}
	}
	if (msg.custom1 == MSG_CUSTOM1_GET_PROXY_URL_SUCCESS && strlen(msg.data)) {
		strcpy(proxyURL, msg.data);
		printf("ask for video success\n");
		return 0;
	}
	printf("data invalid!\n");
	memset(&msg, 0x0, sizeof(MsgStruct));

    return 0;
}

//服务器ip,port
//data格式：用户名+mac, userLen用户名长度
void PlayVideo(int fd, char *user, char *mac)
{
	int userLen;
	char data[128];
    char proxyURL[64];
    char command[256];
	userLen = strlen(user);
	sprintf(data, "%s%s", user, mac);
    printf("data:%s\n", data);
    int ret = AskForVideo(fd, data, userLen, proxyURL);
	if (ret == -1)
	{
		printf("failed ask for video.\n");
		return;
	}
	printf("proxyURL:%s\n", proxyURL);
    sprintf(command, "%s %s", "vlc", proxyURL);
    ret = system(command);
	if (ret != 0)
	{
		printf("play error.\n");
	}
    return;
}
